package cc.vv.baselibrary.view;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.ref.WeakReference;
import java.util.List;

import cc.vv.baselibrary.R;
import cc.vv.lkbasecomponent.util.LKDialogUtil;
import cc.vv.lkbasecomponent.util.LKScreenUtil;

/**
 * 列表类型Dialog
 *
 * @author: NPF
 * @date: 2018/3/23.
 */
public class DialogListView {
    private static DialogListView mInstance;
    private WeakReference<Activity> mActivity;

    private DialogListView() {
    }

    /**
     * 单例模式获取实例
     */
    public static DialogListView getInstance() {
        if (null == mInstance) {
            synchronized (DialogListView.class) {
                if (null == mInstance) {
                    mInstance = new DialogListView();
                }
            }
        }
        return mInstance;
    }

    /**
     * 初始化D
     *
     * @param activity
     * @param dialogListObjs
     * @param operateInter
     * @return
     */
    public Dialog initDialog(Activity activity, List<DialogListObj> dialogListObjs, OperateInter operateInter) {
        if (null == dialogListObjs || null == activity || null == operateInter) {
            return null;
        }
        mActivity = new WeakReference<>(activity);
        View rootView = LayoutInflater.from(mActivity.get()).inflate(R.layout.dialog_listview, null);
        Dialog dialog = LKDialogUtil.getDialog(rootView, 0, 0, Gravity.CENTER, true);
        LinearLayout ll_dlv_root = rootView.findViewById(R.id.ll_dlv_root);
        ll_dlv_root.removeAllViews();
        for (DialogListObj dialogListObj : dialogListObjs) {
            View childView = LayoutInflater.from(mActivity.get()).inflate(R.layout.dialog_listview_item, null);
            TextView tv_dlvi_item = childView.findViewById(R.id.tv_dlvi_item);
            tv_dlvi_item.setText(dialogListObj.des);
            ll_dlv_root.addView(childView);
            initChildViewClick(childView, dialog, operateInter, dialogListObj);
        }
        Window window = dialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = LKScreenUtil.getScreenWidth()- LKScreenUtil.dp2px(50);
        dialog.onWindowAttributesChanged(attributes);
        dialog.show();
        return dialog;
    }

    /**
     * 初始化子条目点击事件
     *
     * @param childView
     * @param dialog
     * @param operateInter
     * @param dialogListObj
     */
    private void initChildViewClick(View childView, final Dialog dialog, final OperateInter operateInter, final DialogListObj dialogListObj) {
        childView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                operateInter.onItemClick(dialogListObj);
            }
        });
    }

    /**
     * 条目点击回调
     */
    public interface OperateInter {
        void onItemClick(DialogListObj dialogListObj);
    }

    /**
     * 列表实体
     */
    public static class DialogListObj {
        public DialogListObj(int type, String des) {
            this.type = type;
            this.des = des;
        }

        /**
         * 操作类型
         */
        public int type;
        /**
         * 描述
         */
        public String des;

        @Override
        public String toString() {
            return "DialogListObj{" +
                    "type=" + type +
                    ", des='" + des + '\'' +
                    '}';
        }
    }


}
